package com.maserb.monitor.web.schoolForum;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolForum.CommentEntity;
import com.maserb.monitor.entity.schoolForum.CommentReplyEntity;
import com.maserb.monitor.entity.schoolForum.PostEntity;
import com.maserb.monitor.enums.*;
import com.maserb.monitor.service.author.AuthorService;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.schoolForum.CommentReplyService;
import com.maserb.monitor.service.schoolForum.CommentService;
import com.maserb.monitor.service.schoolForum.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学校论坛后台管理
 */
@Controller
@RequestMapping("/forumPost")
public class SchoolPostController {


    @Autowired
    PostService postService;

    @Autowired
    CommentService commentService;

    @Autowired
    CommentReplyService replyService;

    @Autowired
    IToolService toolService;

    @Autowired
    AuthorService authorService;

    @RequestMapping(value = {"/", "/index"})
    public ModelAndView getIndex(final HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("forum/index");
        SchoolPostTypeEnum[] typeEnums = SchoolPostTypeEnum.values();
        view.addObject("typeEnums",typeEnums);
        return view;
    }

    /**
    * @Description:  论坛列表
    * @Param: [page, keyword]
    * @return: com.maserb.monitor.common.PageResponseResult<com.maserb.monitor.entity.schoolForum.PostEntity>
    * @Author: Mr.Wang
    * @Date: 2018/8/8
    */
    @RequestMapping("/postList")
    @ResponseBody
    public PageResponseResult<PostEntity> postList(HttpServletRequest request,PageRequestParam page, String keyword) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<PostEntity>>() {
            @Override
            public Page<PostEntity> invoke(PageRequest pageRequest) throws Exception {
                String schoolId = "";
                HttpSession session = request.getSession(true);
                UserEntity user = (UserEntity) session.getAttribute("user");
                if(null!=user){
                    schoolId = user.getOrgId();
                }
                Page<PostEntity> page = postService.findAll(pageRequest, keyword,schoolId);
                for(PostEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getType() && !"".equals(entity.getType())){
                        entity.setType(SchoolPostTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                    if(null!=entity.getIsTop() && !"".equals(entity.getIsTop())){
                        entity.setIsTop(SchoolIsNotTypeEnum.getNameByValue(Integer.parseInt(entity.getIsTop())));
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/doTop")
    @ResponseBody
    public AjaxResult<Boolean> doTop(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id -> postService.doTop(id));
                return true;
            }
        });
    }

    @RequestMapping("/cancelTop")
    @ResponseBody
    public AjaxResult<Boolean> cancelTop(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id -> postService.cancelTop(id));
                return true;
            }
        });
    }

    /**
     * 发帖
     * @param entity
     * @return
     */
    @RequestMapping("/postSave")
    @ResponseBody
    public AjaxResult<Boolean> postSave(PostEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setIsTop("0");
                entity.setCreateTime(new Date());
                postService.save(entity);
                return true;
            }
        });
    }

    /**
     * 评论
     * @param postId
     * @return
     */
    @RequestMapping("/commentList")
    @ResponseBody
    public PageResponseResult<CommentEntity> commentList(PageRequestParam page,final String postId) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<CommentEntity>>() {
            @Override
            public Page<CommentEntity> invoke(PageRequest pageRequest) throws Exception {
                Page<CommentEntity> page = commentService.findAll(pageRequest, postId);
                for(CommentEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getType() && !"".equals(entity.getType())){
                        entity.setType(SchoolCommentTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult<Boolean> doDelete(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                postService.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteItem")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteItem(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                commentService.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    /**
     * 评论
     * @param entity
     * @param postId
     * @return
     */
    @RequestMapping("/commentSave")
    @ResponseBody
    public AjaxResult<Boolean> commentSave(CommentEntity entity, String postId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                PostEntity post = postService.findOne(postId);
                entity.setCreateTime(new Date());
                entity.setPost(post);
                commentService.save(entity);
                return true;
            }
        });
    }

    /**
     * 回复
     * @param page
     * @param commentId
     * @return
     */
    @RequestMapping("/commentReplyList")
    @ResponseBody
    public PageResponseResult<CommentReplyEntity> commentReplyList(PageRequestParam page,final String commentId) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<CommentReplyEntity>>() {
            @Override
            public Page<CommentReplyEntity> invoke(PageRequest pageRequest) throws Exception {
                Page<CommentReplyEntity> page = replyService.findAll(pageRequest, commentId);
                for(CommentReplyEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if(null!=entity.getType() && !"".equals(entity.getType())){
                        entity.setType(SchoolCommentReplyTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                }
                return page;
            }
        });
    }

    @RequestMapping("/doDeleteOption")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteOption(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                replyService.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    /**
     * 评论
     * @param entity
     * @param commentId
     * @return
     */
    @RequestMapping("/commentReplySave")
    @ResponseBody
    public AjaxResult<Boolean> commentReplySave(final CommentReplyEntity entity, String commentId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                CommentEntity comment = commentService.findOne(commentId);
                entity.setCreateTime(new Date());
                entity.setComment(comment);
                replyService.save(entity);
                return true;
            }
        });
    }
}
